package awt;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @description: 监听器
 * @author: Tom
 * @create: 2019-07-26 17:12
 **/
public class TestAddListener implements ActionListener, WindowListener {
    Frame f;
    Button b;

    private void create() {
        f = new Frame("Add Listener");
        b = new Button("press me");
        //为按钮注册监听器
        b.addActionListener(this);
        //为窗口注册监听器
        f.addWindowListener(this);
        f.add(b, "North");
        f.setVisible(true);
        f.setSize(200, 200);

    }

    public static void main(String[] args) {
        TestAddListener testAddListener = new TestAddListener();
        testAddListener.create();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("the Button is Pressed ");

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    /**
     * 实现窗口关闭功能
     * @param e
     */
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(1);

    }

    @Override
    public void windowClosed(WindowEvent e) {


    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
